<?php
declare (strict_types=1);

namespace app\admin\controller;

use app\common\model\AccountCate;
use think\App;

use app\common\builder\TableBuilder;
use app\common\builder\TableFront;

class SummaryUser extends Base
{
    //主键，默认为id
    protected $pk = 'id';

    //关联模型
    protected $hasOne = ['user'];

    //主模型
    protected $model = '\app\common\model\SummaryUser';

    public function index()
    {
        return $this->setTable();
    }

    public function setTable()
    {
        $tableFront = TableFront::getInstance()
            ->addRow(['field' => 'code', 'title' => '编号', 'fixed' => 'left', 'templet' => '<div>{{d.user.code}}</div>', 'totalRowText' => '合计'])
            ->addRow(['field' => 'nickname', 'title' => '昵称', 'templet' => '<div>{{d.user.nickname}}</div>'])
            ->addRow(['field' => 'mobile', 'title' => '手机号', 'width' => 120, 'templet' => '<div>{{d.user.mobile}}</div>']);
        foreach (AccountCate::cateList() as $v) {
            $tableFront->addRow(['field' => 'sum_'.$v['id'], 'width' => 120, 'templet' => '<span>{{d.sum_' . $v['id'] . '}}</span>', 'title' => $v['name'], 'totalRow' => true]);
        }
        $tableFront->addRow(['field' => 'update_time', 'title' => '更新时间', 'width' => 150]);
        $tableFront->addRow(['fixed' => 'right', 'align' => 'center', 'toolbar' => '#barDemo', 'title' => '操作', 'width' => 100]);

        return TableBuilder::getInstance()
            ->addColumns($tableFront)
            ->setSearch($this->setSearch(), false)
            ->addTopButtons(['export'])
            ->addRightButtons([
                'summary_info' => [
                    'title' => '查看明细',
                    'type' => 'summary_info',
                    'url' => url('summary_info')->build()
                ]
            ])
            ->setTotalRaw('true')
            ->addJsUrl(['summary'])
            ->fetch('summary/index');
    }

    public function setSearch()
    {
        return TableFront::getInstance()
            ->setSearch(['uid', '会员编号/昵称/手机号', 'input', '', '\app\common\model\User', 'code|nickname|mobile,id']);

    }

    public function summary_info($id)
    {
        $info = \app\common\model\SummaryUser::find($id);
        $url = url('account/bonus') . '?uid=' . $info->user->code . '&summary=1';
        return redirect($url);

    }

    public function export()
    {
        $s = $this->setMap($this->request->get('name'));
        $s['hasOne'] = $this->hasOne;
        $title = TableFront::getInstance()->getFieldTable($this->request->get('name'));

        if (empty($title)) {
            return json(['code' => -1, 'msg' => '导出失败，请清除缓存后重试！']);
        }
        $s['filed'] = 'uid,sum_1,sum_2,update_time';
        $list = $this->model::getSelect($s);
        foreach ($list['data'] as &$v) {

            $v['code'] = $v->user->code;
            $v['nickname'] = $v->user->nickname;
            $v['mobile'] = $v->user->mobile;
            unset($v->uid);
        }
        $name = '奖金总表(按会员统计)' . date('Y-m-d H:i:s');

        return json(['code' => 1, 'result' => ['data' => $list['data']->toArray(), 'title' => $title, 'name' => $name]]);

    }
}
